#
# book2html.pl
#
# Copyright 1996-2002, Silicon Graphics, Inc.
# All Rights Reserved.
#
# This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
# the contents of this file may not be disclosed to third parties, copied or
# duplicated in any form, in whole or in part, without the prior written
# permission of Silicon Graphics, Inc.
#
# RESTRICTED RIGHTS LEGEND:
# Use, duplication or disclosure by the Government is subject to restrictions
# as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
# and Computer Software clause at DFARS 252.227-7013, and/or in similar or
# successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
# rights reserved under the Copyright Laws of the United States.
#
# requires perl5 and format2html.pl
#

require 'format2html.pl';


sub book2html {

    my($bk_root, $bk_pth, $in_fname, $in_srch) = @_;

    if( $in_fname eq '' ) {
        print "book2html: filename not specified" if($_WEB == 0);
        return;
    }

    my($fname) = &unescape($in_fname);
    my($srch)  = $in_srch;
    my($cgi)   = $ENV{SCRIPT_NAME} || "/cgi-bin/infosrch.cgi";
    my($coll)  = $COLLECTION;
    $coll = (split(';', $COLLECTION))[0] if ($COLLECTION ne '');
    my($db)    = $_DB;

    $fname =~ s/\#(.*?)$//;


    # create the correct path to the book
    #
    if( ($s = &isBkHTML($bk_root, $bk_pth, $fname)) eq '' ) {

       if( &isBkPDF($bk_root, $bk_pth, $fname) ) {
           return;                     # provide a link
       }
       if( &isBkEBT($bk_root, $bk_pth, $fname, $cgi) ) {
           return;                     # send an error about converting
       }

       &err_str('0');
       return;
    }

    $fname = $s;

    if( $fname !~ /\.htm/ ) {
        &err_str('0');
        return;
    }


    my($shlf, $bk) = '';
    if( $fname =~ /\/([^\/]+)\/books\/([^\/]+)\// ) {
        $shlf = $1; 
        $bk   = $2;
    }

    my($osrch) = $srch;
    $srch = &escape($srch);
    $cgi .= "\?cmd=getdoc&coll=${coll}&db=${db}" .
            ($srch ne '' ? "&srch=${srch}" : '') . "&fname=/${shlf}/${bk}/";

    # now handle the doc itself
    #
    my($pth) = ($bk_root ne '' ? $bk_root : '') . $bk_pth;
    my($i) = index($s, "/library");
    if($i >= 0) {
       $base = substr($fname, $i);
    } else {

       # fallback...try this..
       #
       $base = substr($fname, length($pth));
    }

    $base =~ s/[^\/]*$//;
    &txt2html($pth, $base, $fname, $osrch, $cgi);

    return;
}


sub isBkHTML {

  my($bk_root, $bk_pth, $in_pth) = @_;

  my($base) = ($bk_root ne '' ? $bk_root : '') . $bk_pth;
  my($pth) = &getBkPth($bk_root, $bk_pth, $in_pth);

  my($s, $f) = '';
  my(@c) = split('\/', $pth);
  if( (@c + 0) < 2 ) {
      return '';
  }

  # HTML book; find the proper path
  #
  if( $pth !~ /\.htm/ ) {
      push(@c, "index.html");
  }

  $s = "$base/$c[0]/books/$c[1]/sgi_html/$c[2]";

  # a lower pth into the books dir
  #
  if( (@c + 0) > 3 ) {
      for( $i = 3; $i < (@c + 0); $i++ ) {
           $s .= '/' . $c[$i];
      }
  }

  if( -f "$s" ) {
      $f = $s;
  } else {

      $s =~ s/\/sgi_html\//\/html\//;
      if( -f "$s" ) {
          $f = $s;
      } else {
          $s =~ s/\/html\//\//;
          if( -f "$s" ) {
              $f = $s;
          }
      }
  }

  return $f;
}


sub isBkPDF {

  my($bk_root, $bk_pth, $in_pth) = @_;

  my($base) = ($bk_root ne '' ? $bk_root : '') . $bk_pth;
  my($pth) = &getBkPth($bk_root, $bk_pth, $in_pth);

  my(@c) = split('\/', $pth);
  if( (@c + 0) < 2 ) {
      return '';
  }

  my($s) = "$base/$c[0]/books/$c[1]/pdf/$c[1].pdf";
  if(!(-f $s)) {
       $s =~ s/\/pdf\//\//;
  }

  # provide link to pdf copy of book...
  #
  my($i) = index($s, "/library");
  if (-r $s && $i >= 0) {

      my($f) = substr($s, $i);
      print "<html>\n<body>\n<p>\n&nbsp;Click for a ",
            "<a href=\"${f}\"><b>PDF copy</a></b> of this book.\n",
            "</p>\n</body>\n</html>\n\n";

      return 1;
  }

  return 0;
}


sub isBkEBT {

  my($bk_root, $bk_pth, $in_pth, $cgi) = @_;

  my($base) = ($bk_root ne '' ? $bk_root : '') . $bk_pth;
  my($pth) = &getBkPth($bk_root, $bk_pth, $in_pth);

  my(@c) = split('\/', $pth);
  if( (@c + 0) < 2 ) {
      return '';
  }

  my($s1) = "$base/$c[0]/books/$c[1]/ebt/$c[1].dat";
  my($s2) = "$base/$c[0]/books/$c[1]/index/index.dat";

  if( -f $s1 && -f $s2 && !(-z $s1) && !(-z $s2) ) {

      my($dn, $ttl) = '';
      if( -r "$base/$c[0]/books/$c[1]/booklist.txt" ) {
          open(BKL, "$base/$c[0]/books/$c[1]/booklist.txt");
          my($tmp) = join('', <BKL>);
          close(BKL);
          if( $tmp =~ /.*(\d\d\d-\d\d\d\d-\d\d\d[a-zA-Z]*).*/ ) {
              $dn = $1;
          }
          if( $tmp =~ /ALIAS[\ \=]+\"([^\"]+)\"/i ) {
              $ttl = " <i>(" . $1 . ")</i> ";
          }
      }
 
      print "<html>\n<body>\n<p>\n",
            "<b>Warning</b>:&nbsp; This book ${ttl} cannot be ",
            "read and needs to be converted.\n Refer to the ",
            "<a href=\"${cgi}\?cmd=getdoc&db=man&fname=insightAdmin\">insightAdmin</a> ",
            "command for details on how to convert this book (examine the ",
            "\"-c -d\" options). ";
      if( $dn ne '' ) {
          print "Or try to access the book ${ttl} from the ",
                "<a href=\"http://techpubs.sgi.com/tpl.cgi/info/${dn}\">SGI ",
                "techpubs library</a>.\n";
      }
      print "</p>\n</body>\n</html>\n\n";

      return 1;
  }

  return 0;
}



sub txt2html {

   my($in_root, $in_base, $in_fname, $in_srch, $cgi) = @_;

   if( $in_fname eq '' || $in_root eq ''
       ||
       $in_fname !~ /^$in_root/ || $in_fname !~ /\.htm[l]*$/ ) {
       &err_str('0');
       return;
   }

   open(FP, "$in_fname") || do {
       &err_str('0');
       return;
   };
   my($buf) = join('', <FP>);
   close(FP);

   # title string
   #
   my($ttl) = '';
   if ($in_fname !~ /\/index\./) {
       ($ttl) = ($buf =~ m#title=\"(.*?)\"#is);
       $ttl = "<br><center><b>${ttl}</b></center>\n";
   }

   # trim off top/bottom
   #
   $buf =~ s/^.*<body[^>]*>//si;
   $buf =~ s/<\/body.*$//si;

   # for search highlighting
   #
   &buildExpression($in_srch);

   # for image references
   #
   &addTagExpression("SRC[\ ]*=[\ ]*\"", "SRC=\"$in_base");

   # for href's
   #
   $in_fname =~ /[^\/]+(.*?)$/;
   my($curr_fname) = $1;
   &addTagExpression("HREF=\"\#", "HXXX=\"\#");
   &addTagExpression("HREF=\"http\:", "HXXX=\"http\:");
   &addTagExpression("HREF=\"",   "HREF=\"${cgi}");
   &addTagExpression("HXXX=\"",   "HREF=\"");

   # apply these corrections
   #
   &hilite(\$buf);

   print "\n", $ttl, $buf, "\n";
   return;
}



sub getBkPth {

  my($bk_root, $bk_pth, $in_pth) = @_;
  my($pth) = $in_pth;

  # trim all this stuff out for now...isolate shelf, book, file
  #
  if( $bk_root ne '' && $pth =~ /^${bk_root}/) {
      $pth =~ s/^${bk_root}//;
  }
  if( $bk_pth ne ''  && $pth =~ /^${bk_pth}/) {
      $pth =~ s/^${bk_pth}//;
  }
  $pth =~ s/^\///; $pth =~ s/\/$//;

  # we'll put this in later as well...
  #
  if( $pth =~ /\/books\// ) {
      $pth =~ s/\/books\//\//;
  }

  return $pth;
}


sub err_str {
  my($i) = @_;
  my(%s) = { '0' => "Document not specified or invalid. Cannot be accessed." };
  print "<html>\n<body>\n<p>\n", $s{'0'},
        "</p>\n</body>\n</html>\n\n" if($_WEB == 0);
}


# Return true from package include
#
1;

